package com.ss.android.common.applog;

import android.os.Process;
import android.support.design.a;

import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.ToolUtils;

import org.json.JSONObject;

import java.io.Closeable;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Collections;

final class y implements Thread.UncaughtExceptionHandler {
    y() {

    }

    public final void uncaughtException(Thread arg9, Throwable arg10) {
        FileOutputStream v1_3 = null;
        File v4;
        JSONObject v3;
        int v2 = 5;
        Closeable v0 = null;
        if (arg10 == null) {
        } else if (v.a() == null) {
        } else {
            try {
                v3 = i.a(v.a(), arg10);
                String v1_2 = "ss_crash-" + System.currentTimeMillis() + ".log";
                v4 = new File(v.a().getCacheDir().getPath(), "ss_crash_logs");
                if (!v4.exists()) {
                    v4.mkdirs();
                }
                v1_3 = new FileOutputStream(new File(v4, v1_2));
                v1_3.write(v3.toString().getBytes());
                v1_3.close();
                File[] v3_1 = v4.listFiles(v.b());
                if (v3_1 != null && v3_1.length > v2) {
                    Arrays.sort(((Object[]) v3_1), Collections.reverseOrder());
                    int v1_4 = v2;
                    while (true) {
                        if (v1_4 < v3_1.length) {
                            v3_1[v1_4].delete();
                            ++v1_4;
                            continue;
                        }
                        break;
                    }
                }
                a.closeQuietly(v0);
            } catch (Exception v1_1) {
            } finally {
                a.closeQuietly(v1_3);
            }
        }

        if (ToolUtils.isMainProcess(v.a())) {
            if (v.c() == null) {
                return;
            }

            if (v.c() == v.d()) {
                return;
            }
            v.c().uncaughtException(arg9, arg10);
            return;
        }

        try {
            if (Logger.debug()) {
                Logger.d("process", "uncaughtException kill myself");
            }
            Process.killProcess(Process.myPid());
        } catch (Throwable v0_1) {
        }
    }
}

